<?php
namespace App\Models\System;

use App\Exceptions\ApiException;
use App\Providers\LengthAwarePaginatorProvider;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Enums\System\SysJob\{MisfirePolicy, Concurrent, Status};

/**
* 定时任务调度表 模型
* @create 2023-08-22 10:02:14
*/
class SysJobModel extends \App\Models\BaseModel {
    use HasFactory;


    const Table         = "sys_job";
    const PrimaryKey    = "job_id";
    public $timestamps  = true;


    static $Wheres = ['job_id','misfire_policy','concurrent','status'];     // 直接定义column, 默认where(column, '=', value)
    public function __construct($attributes = []) {
        parent::__construct($attributes);

        // static::$Wheres['job_id']    = fn($q, $v) => $q->where(atn('job_id'), '=', $v);
        static::$Wheres['job_name']     = fn($q, $v) => $q->where(atn('job_name'), 'like', "%{$v}%");
        static::$Wheres['job_group']    = fn($q, $v) => $q->where(atn('job_group'), 'like', "%{$v}%");
    }

    /** xxxx */
    // public function user() {
        // return $this->hasOne(User::class, 'id', 'uid');
    // }

    /** ========== 常用查询 ========== */
    static $Vo  = ['*', ];
    static $Voi = ['*', ];

    /** 根据JobId获取 */
    public function selectByJobId($job_id, $job_name=null, $job_group=null): SysJobModel {
        $where = [['job_id', '=', "{$job_id}"]];
        if ($job_name) $where[] = ['job_name', 'like', "%{$job_name}%"];
        if ($job_group) $where[] = ['job_group', 'like', "%{$job_group}%"];
        return throw_str(
            static::row($where) ?? "数据($job_id)为空~"
        );
    }
    /** 根据JobName获取 */
    public function selectByJobName($job_name): SysJobModel {
        $where = [['job_name', 'like', "%{$job_name}%"]];
        return throw_str(
            static::row($where) ?? "数据($job_name)为空~"
        );
    }
    /** 根据JobGroup获取 */
    public function selectByJobGroup($job_group): SysJobModel {
        $where = [['job_group', 'like', "%{$job_group}%"]];
        return throw_str(
            static::row($where) ?? "数据($job_group)为空~"
        );
    }


    /** 获取列表 @return SysJob[]+paginator() */
    public function selectList(int|array $pag=1, array $param=[]): LengthAwarePaginatorProvider {
        return static::rows_pag($pag, [], fn($q)=>$q
                    // ->when(isset($param['job_id']), fn($q)=>$q->where('job_id', '=', $param['job_id']))
                    ->wheres($param, 'job_id','job_name','job_group','misfire_policy','concurrent','status')
                    ->orderByDesc("job_id")
        );
    }


    /** 插入数据 */
    public function insertData(array $data): SysJobModel {
        return throw_str(
            static::row_add([
                // 'job_id' => $data['job_id'],
                // 'job_name' => $data['job_name'] ?? '',
                // 'job_group' => $data['job_group'] ?? 'DEFAULT',
                'invoke_target' => $data['invoke_target'],
                'cron_expression' => $data['cron_expression'] ?? '',
                'misfire_policy' => $data['misfire_policy'] ?? '3',
                'concurrent' => $data['concurrent'] ?? '1',
                'status' => $data['status'] ?? '0',
                'create_by' => $data['create_by'] ?? '',
                // 'create_time' => $data['create_time'] ?? '',
                'update_by' => $data['update_by'] ?? '',
                // 'update_time' => $data['update_time'] ?? '',
                'remark' => $data['remark'] ?? '',
            ])
        );
    }


    /** 更新数据 */
    public function updateData(int|array $idOrWhere, array $data): SysJobModel {
        $values = static::when(
                    $data, 
                    // 'job_id',
                    // 'job_name',
                    // 'job_group',
                    'invoke_target',
                    'cron_expression',
                    'misfire_policy',
                    'concurrent',
                    'status',
                    'create_by',
                    // 'create_time',
                    'update_by',
                    // 'update_time',
                    'remark',
                );
        return throw_str(
            static::row_update($idOrWhere, $values)
        );
    }


    /** 删除数据 */
    public function deleteData(int|array $idOrWhere): SysJobModel {
        return throw_str(
            static::row_delete($idOrWhere)
        );
    }



}